package com.qf.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.*;
import com.qf.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("repair")
public class RepairController {

    // 保修对象
    @Autowired
    private RepairService repairService;


    // 保修管理
    @RequestMapping("repairList")
    @ResponseBody
    public JSONResult repairList(@RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "limit", required = false, defaultValue = "5") Integer pageSize
                            ) {

        // 设置分页条件
        PageHelper.startPage(pageNum,pageSize);

        List<Repair> repairList = repairService.findByRepairPage();

        PageInfo<Repair> pageInfo = new PageInfo<>(repairList);

        JSONResult jsonResult = new JSONResult();
        jsonResult.setCode(0);
        jsonResult.setCount(pageInfo.getTotal());
        jsonResult.setMsg("success");
        jsonResult.setData(repairList);
//        System.out.println(jsonResult);
        return jsonResult;


//        model.addAttribute("pageInfo",pageInfo);
//
//        return "listrepair.html";
    }


    // 添加
    @RequestMapping("addRepair1")
    @ResponseBody
    public JSONResult addRepair1() {

        List<Community> communityList = repairService.findAllCommunity();

        JSONResult jsonResult = new JSONResult();
        System.out.println("=================================================================================================");
        jsonResult.setData(communityList);

        return jsonResult;
    }
    // 添加
    @RequestMapping("addRepair2")
    @ResponseBody
    public JSONResult addRepair2() {

        List<Owner> ownerList = repairService.findAllOwner();

        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(ownerList);

        return jsonResult;
    }
    // 添加
    @RequestMapping("addRepair3")
    @ResponseBody
    public JSONResult addRepair3() {

        List<Equipment> equipmentList = repairService.findAllEquipment();

        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(equipmentList);

        return jsonResult;
    }

    // 添加或者修改之后跳转
    @RequestMapping("saveOrUpdateRepair")
    @ResponseBody
    public JSONResult saveOrUpdateRepair(Repair repair) {


        System.out.println(repair);

        JSONResult jsonResult = new JSONResult();
        jsonResult.setCode(10000);
        repairService.saveOrUpdateRepair(repair);

        return jsonResult;
    }



    // 修改
    @RequestMapping("updateRepair")
    @ResponseBody
    public  JSONResult updateRepair(Integer id) {

        Repair repair = repairService.findByRepairId(id);

        System.out.println(repair);
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(repair);

        return jsonResult;
    }


    // 删除
    @RequestMapping("deleteRepair")
    @ResponseBody
    public String deleteRepair(Integer id) {

        repairService.deleteRepair(id);

        return "success";
    }

    // 批量删除

    @RequestMapping("deleteByRepairId")
    @ResponseBody
    public String deleteByRepairId(Integer[] ids) {
        repairService.deleteByRepairId(ids);
        return "success";
    }

    // 多条件查询


}
